/**
 * @filename:CapitalTypeServiceImpl 2023-4-20
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.MoldAllocation;
import com.wonder.app.domain.MoldScrap;
import com.wonder.app.mapper.MoldAllocationDao;
import com.wonder.app.mapper.MoldScrapDao;
import com.wonder.app.request.MoldAllocationRequest;
import com.wonder.app.request.MoldScrapRequest;
import com.wonder.app.service.MoldAllocationService;
import com.wonder.app.service.MoldAndInspectService;
import com.wonder.app.service.MoldScrapService;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Description:TODO(模具调拨单类型服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 *
 */
@Service

public class MoldAllocationServiceImpl extends ServiceImpl<MoldAllocationDao, MoldAllocation> implements MoldAllocationService {
    @Autowired
    private MoldAllocationService moldAllocationService;
    @Autowired
    private MoldAllocationDao moldAllocationDao;
    @Override
    public String validate(MoldAllocation moldAllocation, String type) {
        return "";
    }

    @Override
    public IPage<MoldAllocation> selectList(MoldAllocationRequest request, Page<MoldAllocation> page) {
        LambdaQueryWrapper<MoldAllocation> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (null != request.getId()) {
            lambdaQueryWrapper.eq(MoldAllocation::getId, request.getId());
        }


        lambdaQueryWrapper.orderByDesc(MoldAllocation::getId);
        Page<MoldAllocation> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    /**
     *报废调拨单
     * @param moldScrap
     * @return
     */
    @Override
    public MoldAllocation createMoldAllocation(String userName, MoldScrap moldScrap){
        String BILLNO =  IXqcConst.createBillNo(BillType.BFDB);//生成报废调拨单号
        MoldAllocation moldAllocation = new MoldAllocation();
        moldAllocation.setBillNo(BILLNO);
        moldAllocation.setCreateTime(new Date());
        moldAllocation.setMaterialName(moldScrap.getMaterialName());
        moldAllocation.setMaterialNo(moldScrap.getMaterialNo());
        moldAllocation.setStockNo(moldScrap.getStockNo());
        moldAllocation.setLocationNo(moldScrap.getLocationNo());
        moldAllocation.setToStockNo("报废仓库");
        //需要确认报废仓库是否启用仓位
        moldAllocation.setToLocationNo("报废仓位");
        //创建调拨单
        if(moldAllocationService.saveOrUpdate(moldAllocation)){
            return moldAllocation;
        }else {
            return null;
        }
    }
}
